package com.cc8w.home.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.cc8w.entity.UserEntity;
import com.cc8w.exception.MyException;
import com.cc8w.home.service.UserService;
import com.github.pagehelper.PageHelper;



@Controller
@RequestMapping("/home/index")
public class IndexController {
	
	@Autowired
	public UserService userService;
	
	
	@RequestMapping("index1.do")
	public String index1() {
		return "112";
	}
	
	
	//@Autowired
    //private UserService userService;
	/**
	 * 首页方法  
	 * /home/index/index.do
	 */
	@RequestMapping("/index")
	public @ResponseBody UserEntity index() {	
        UserEntity userPojo = userService.queryUserByUserName("fps2tao");
        System.out.println(userPojo);
		return userPojo;
	}
	
	@RequestMapping("/getall")
	public @ResponseBody List<UserEntity> getall(HttpServletRequest req,HttpServletResponse resp,@RequestParam(value = "pageNum",defaultValue="1") int pageNum,@RequestParam(value = "pageSize",defaultValue="5") int pageSize) throws MyException {

		PageHelper.startPage(pageNum, pageSize);
		List<UserEntity> userPojo = userService.getAll();
        System.out.println(userPojo);
        
        
        if(pageNum==-1) {
           throw new MyException();
        }
        resp.setContentType("application/json;charset=utf-8");
		return userPojo;
	}
	
	@RequestMapping("/extest")
	public String exTest(@RequestParam(value = "i",defaultValue="1") int i) {
		
		if(i==-1) {
			return "redirect:error";
		}
		
		return null;
	}
	
	/**
	 * 自定义异常显示页面
	 * 使用ResponsStatusExceptionResolver,对应的注解 @ResponseStatus
	 */
	@RequestMapping("/error")
	@ResponseStatus(value=HttpStatus.FORBIDDEN,reason="测试错误提示func")
	public String exTest() {
		return "error";
		
	}
	

}
